/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dondathang.java
 *
 * Created on Nov 8, 2011, 2:08:48 PM
 */
package GUI;

import BLL.B_dathang;
import BLL.B_thuchi;
import BLL.sanpham;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import BLL.B_dondathang;
import BLL.sanpham;
import DBH.DBHelper;
import BLL.Export;
import GUI.Nhanvien;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author 
 */
public class dondathang_1 extends javax.swing.JFrame {

    DefaultTableModel model;
    DefaultTableModel model1;
    String usr;
    String pwd;
    // private Vector v;
    sanpham sp;
    B_dathang dh;
    B_dondathang dh1;
    B_thuchi tc;
    /** Creates new form dondathang */
    public dondathang_1(String ten, String pass) {
        this.usr = ten;
        this.pwd = pass;
        initComponents();
        model = (DefaultTableModel) jTable2.getModel();
        model1 = (DefaultTableModel) jTable1.getModel();
     //   jPanel1.setVisible(false);
        jLabel3.setText(ten);
        display();

    }

    public void loadTable1(Vector v) {
        int k1 = model.getRowCount();
        for (int j = 0; j < k1; j++) {
            if (model.getRowCount() != 0) {
                model.removeRow(0);
            }
        }
        for (int i = 0; i < v.size(); i++) {
            dh = (B_dathang) v.get(i);
            Vector k = new Vector();
            k.addElement(dh.getIddathang());
            k.addElement(dh.getNgaydat());

            model.addRow(k);

        }
        jTable2.setModel(model);
    }

    public void display() {
        dh = new B_dathang();
        Vector v1 = new Vector();
        try {
            v1 = dh.getDathang();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable1(v1);
    }

    public void loadTable2(Vector v) {
        int k1 = model1.getRowCount();
        for (int j = 0; j < k1; j++) {
            if (model1.getRowCount() != 0) {
                model1.removeRow(0);
            }
        }
        for (int i = 0; i < v.size(); i++) {
            dh1 = (B_dondathang) v.get(i);
            Vector k = new Vector();
            k.addElement(dh1.getIddathang());
            k.addElement(dh1.getIdsp());
            k.addElement(dh1.getTensanpham());
            k.addElement(dh1.getSoluong());
             k.addElement(dh1.getGiatien());
            model1.addRow(k);

        }
        jTable1.setModel(model1);
    }

    public void display2(String id) {
        dh1 = new B_dondathang();
        Vector v1 = new Vector();
        try {
            v1 = dh1.getDondathangid(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable2(v1);
    }
   public void seach1(){
       int i = jTable2.getSelectedRow();
        dh1=new B_dondathang();
        Vector v=new Vector();        
        try {
            v=dh1.getDondathangid(String.valueOf(jTable2.getValueAt(i,0).toString()));
        } catch (SQLException e) {
            e.printStackTrace();
        }  
        this.display2(String.valueOf(jTable2.getValueAt(i,0)));
     }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor. 
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        jLabel4.setText("jLabel4");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã hóa đơn", "Mã sản phẩm", "Tên sản phẩm", "Số lương", "Giá"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Tổng tiền");

        jButton1.setText("Thanh toán");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Hủy bỏ");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(jButton1)
                        .addGap(54, 54, 54)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(117, 117, 117)
                        .addComponent(jLabel1)
                        .addGap(30, 30, 30)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Hãy chọn hóa đơn để thanh toán");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã hóa đơn", "Ngày đặt hàng"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jButton5.setText("Chi tiết");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Hủy bỏ");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jButton5)
                        .addGap(38, 38, 38)
                        .addComponent(jButton6)))
                .addContainerGap(58, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton5))
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(69, 69, 69))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    int i = jTable2.getSelectedRow();
    if(i==-1)
    JOptionPane.showMessageDialog(rootPane,"Vui lòng chọn hóa đơn để thanh toán");
else{
       
this.seach1();
}
    int j = jTable1.getRowCount();
      int tongtien = 0;
          
                for (int a = 0; a <= j; a++) {
                    String gia = (jTable1.getValueAt(a, 4)).toString();
                    int gia1 = Integer.parseInt(gia);
                    String sl = (jTable1.getValueAt(a, 3)).toString();
                    int sl1 = Integer.parseInt(sl);
                    int tien = (gia1 * sl1);
                    tongtien = tongtien + tien;
                    jTextField1.setText(String.valueOf(tongtien));

                }
    
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
 this.dispose();
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    Export exp = new Export();
    String detail = exp.openFile("model_1.dat");
                    int k=jTable1.getRowCount();
                    String tt=jTextField1.getText();
    detail = detail + "<html><h1 align=center>Cua Hang Noi That Song Hong</h1></br><h3 align=right>Ngay.............thang.............nam...........</h3></br></br>";
    detail = detail + "<h1 align=center>Hoa Don Thanh Toan</h1>";
    detail = detail +	"<table border=1 align=center width=400>";
    detail = detail +	"	<tr>";
		detail = detail +	"<th>Ma ha don</th>";
		detail = detail +	"<th>Ma sp</th>";
		detail = detail +	"<th>Ten sp</th>";
		detail = detail +	"<th>So Luong</th>";
		detail = detail +	"<th>Gia</th>";
		detail = detail +       "</tr>";
                                for (int i = 0; i < k; i++) {
                    String a1= jTable1.getValueAt(i, 0).toString();
                    String a2=  jTable1.getValueAt(i, 1).toString() ;
                    String a3=   jTable1.getValueAt(i, 2).toString();
                    String a4=   jTable1.getValueAt(i, 3).toString()  ;
                    String a5=  jTable1.getValueAt(i, 4).toString()  ; 
                                detail = detail +	"<tr>";
    
                                detail = detail +"<td >"+ a1 + "</td>";
                                detail = detail +"<td>" + a2 + "</td>";
                                detail = detail +"<td>" + a3 + "</td>";
				detail = detail +"<td>" + a4 + "</td>";
				detail = detail +"<td>" + a5 + "</td>";
                                
			detail = detail +"</tr>";
                                }

	detail = detail +"</table>";
         detail = detail + "<h3 align=center>Tong Tien: "+tt+"Vnd</h3>";
        detail = detail + "<h3 align=left>Nhan Vien Ky</h3>";
        detail = detail + "<h3 align=right>Khach Hang Ky</h3>";
        detail = detail +"</html>";
    String URL = null;
    JFileChooser jfc = new JFileChooser("C:\\");
    int j = jfc.showSaveDialog(null);
    if (j == JFileChooser.APPROVE_OPTION) {
        URL = String.valueOf(jfc.getSelectedFile());
    }
    exp.reportApplication(detail.getBytes(), URL);
    if (j == 0) {
        JOptionPane.showMessageDialog(null, "Xuất hóa đơn thành công");
        
        
        /// Xóa sản phẩm đã thanh toán
        for (int i = 0; i < k; i++) {
                    String a1= jTable1.getValueAt(i, 0).toString();
                    String a2=  jTable1.getValueAt(i, 1).toString() ;
                    String a3=   jTable1.getValueAt(i, 2).toString();
                    String a4=   jTable1.getValueAt(i, 3).toString()  ;
                    String a5=  jTable1.getValueAt(i, 4).toString()  ; 
                    int so = Integer.parseInt(a4);
                    sp = new sanpham();
                sp.setIdsp(a2);
                sp.setSoluong(so);
                int t = 0;
                try {
                    t = sp.updatedelete(sp);
                } catch (SQLException sqle) {
                    sqle.printStackTrace();
                }
        }
        
        ////
        String b = jLabel3.getText();
        tc = new B_thuchi();
                    tc.setIdnv(b);
                    tc.setTongtien(tt);    
                    int t = 0;
                    try {
                        t = tc.Insertthuchi(tc);
                    } catch (SQLException ex) {
                        Logger.getLogger(Nhanvien.class.getName()).log(Level.SEVERE, null, ex);
                    }
        
               int p = model1.getRowCount();
        for (int i = 0; i <= p - 1; i++) {
            if (p != 0) {
                model1.removeRow(0);
            }
        } 
        jTextField1.setText("");
    }
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Dondathang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Dondathang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Dondathang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Dondathang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Dondathang(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
